/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display;

import deatax.GameData;
import deatax.Player;
import deatax.Resource;
import deatax.Tile;
import deatax.display.gui.BevelBorder;
import deatax.display.gui.JShadowLabel;
import deatax.display.gui.PlayerInfoPanel;
import deatax.display.gui.RoundedPanel;
import deatax.display.gui.TileInfoPanel;
import deatax.network.Client;
import deatax.network.Messages;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

/**
 *
 * @author pson
 */
public class GUI {

    protected Dimension preferred_item;
    protected Dimension preferred_resource;
    protected int sidebar_width;
    protected int message_height;
    protected Icon cancel_icon;
    protected GameData data;
    
//    protected JPanel side_bar;
//    protected GLJPanel side_bar_map;
//    protected JPanel side_bar_cards;
//    protected JPanel side_bar_details;
//    protected JPanel side_bar_player;
//    protected JPanel side_bar_finish;

    protected JPanel pnlCenter;
    protected JPanel pnlPlayersBar;
    
    protected JButton btnEndTurn = new JButton("End Turn");

    protected JShadowLabel lblMode;
    public JLabel lblFPS = new JLabel();

    protected JPanel pnlMultidisplay;
    
    protected JPanel bottom_bar_main;
   
    
    protected TileInfoPanel tile_info;
    protected PlayerInfoPanel player_info;
    
    protected JScrollPane message_scroller;
    protected JEditorPane message_pane;
    protected StringBuilder message_builder;
    protected HashMap<Resource, JLabel> player_resources;

    public GUI(GameData data) {
        this.data = data;
    }
    
    public void gameStart() {
        cancel_icon = new ImageIcon(Texture.loadImage("icons/cancel.png"));
        preferred_item = new Dimension(90, 25);
        preferred_resource = new Dimension(50, 25);
        sidebar_width = 225;
        message_height = 180;

        //context_menu.setLightWeightPopupEnabled(false);

//        side_bar = new RoundedPanel();
//        side_bar.setLayout(new GridLayout(0, 1));
//        side_bar.setPreferredSize(new Dimension(sidebar_width, 800));
        

        pnlCenter = new JPanel();
        pnlCenter.setLayout(new GridBagLayout());
        pnlCenter.setOpaque(false);
        pnlCenter.setBorder(new BevelBorder(true,true));
        

        pnlPlayersBar = new JPanel();
        pnlPlayersBar.setLayout(new BorderLayout());
        pnlPlayersBar.setOpaque(false);
        pnlPlayersBar.setPreferredSize(new Dimension(100,23));
        pnlPlayersBar.setMinimumSize(pnlPlayersBar.getPreferredSize());

        lblMode = new JShadowLabel();
        lblMode.setText("Placement Mode");
        pnlPlayersBar.add(lblMode, BorderLayout.CENTER);
        lblFPS.setPreferredSize(new Dimension(120,23));
        pnlPlayersBar.add(lblFPS, BorderLayout.WEST);

        
        btnEndTurn.addActionListener(new FinishListener());
        btnEndTurn.setPreferredSize(new Dimension(80,23));
        pnlPlayersBar.add(btnEndTurn, BorderLayout.EAST);
        

        pnlMultidisplay = new RoundedPanel();
        pnlMultidisplay.setBackground(Color.white);
        pnlMultidisplay.setLayout(new CardLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = gbc.gridy = 0;
        gbc.weightx = 1;
        gbc.insets = new Insets(2,2,2,2);
        gbc.fill = GridBagConstraints.BOTH;
        pnlCenter.add(pnlPlayersBar,gbc);
        gbc.gridy = 1;
        gbc.weighty = 1;
        pnlCenter.add(pnlMultidisplay,gbc);

        bottom_bar_main = new RoundedPanel();
        bottom_bar_main.setLayout(new BoxLayout(bottom_bar_main,BoxLayout.X_AXIS));
        
//        turn_panel = new TurnPanel(data.getState().getState("Turn"));
//        turn_panel.setPreferredSize(new Dimension(sidebar_width, message_height));

        message_builder = new StringBuilder();
        message_pane = new JEditorPane("text/html", null);
        message_pane.setEditable(false);
        message_pane.setFont(UIManager.getFont("Label.font"));
        if(message_pane.getEditorKit() instanceof HTMLEditorKit){
            StyleSheet ss = ((HTMLEditorKit)message_pane.getEditorKit()).getStyleSheet();
            ss.addRule("a { text-decoration:none; }");
            ss.addRule("body { font-family:Tahoma; font-size:12pt; }");
            ss.addRule(".column_name { color:#222222; font-weight:bold; }");
            ss.addRule(".RangePart { color:#9900bb; }");
            ss.addRule(".new { color:#009900; text-decoration:none; font-family:Courier New; }");
            ss.addRule(".remove { color:#990000; text-decoration:none; font-family:Courier New; }");
            ss.addRule(".rule { padding-left:10px; color:#333333;}");
        }
//        message_pane.set
        message_scroller = new JScrollPane(message_pane, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        message_scroller.setPreferredSize(new Dimension(800, message_height));
        message_scroller.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        message_scroller.setOpaque(false);
        pnlMultidisplay.add(message_scroller,"Messages");

        tile_info = new TileInfoPanel();
        tile_info.setPreferredSize(new Dimension(sidebar_width, 100));
        tile_info.setMinimumSize(new Dimension(sidebar_width, 0));
        
        player_info = new PlayerInfoPanel();
        player_info.setPreferredSize(new Dimension(sidebar_width, 100));
        player_info.setMinimumSize(new Dimension(sidebar_width, 0));
        
        
        //gbc.anchor = GridBagConstraints.NORTHWEST;
        
//        gbc.weightx = 1;
//        gbc.fill = GridBagConstraints.BOTH;
//        side_bar.add(tile_info);//, gbc);
//        gbc.gridy++;
//        side_bar.add(player_info);//, gbc);
//        gbc.gridy++;

        bottom_bar_main.add(tile_info);
        bottom_bar_main.add(pnlCenter);
        bottom_bar_main.add(player_info);
//        bottom_bar_main.add(turn_panel);

//        this.data.getFrame().add(side_bar, BorderLayout.EAST);
        this.data.getFrame().add(bottom_bar_main, BorderLayout.SOUTH);

    }
    
    public void refreshContent() {
        updatePlayerInfo();
        bottom_bar_main.revalidate();
        bottom_bar_main.repaint();
    }
    
    public void setTileInfo(Tile t) {
        tile_info.setDetails(t, data.getCurrentPlayer());
        tile_info.repaint();
    }

    public void setLabelMode(String label) {
        lblMode.setText(label);
    }
    
    public void setCurrentPlayer(Player p) {
//        lblName.setForeground(p.getColor());
//        lblName.setText(p.getName() + "'s Turn");
    }
    
    public void enableEndTurn(boolean b) {
        btnEndTurn.setEnabled(b);
        if(b){
            data.getTray().notifyTray("It's your turn", true);
        } else {
            data.getTray().notifyTray(null, false);
        }
    }
    
    private class FinishListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
//            if (data.getState().currentState() != null) {
//                data.getState().currentState().finish();
//            }
            Client.get().sendMessage(Messages.END_TURN);
        }
    }
    
    public void addMessage(Player p, String message) {
        String msg = "";
        if (p != null) {
            msg += "<font color=\"#" + Integer.toHexString(p.getColor().getRGB() & 0x00ffffff) + "\">" + p.getName() + ": </font>";
        }
        msg += message + "<br/>";

        message_builder.append(msg);
        message_pane.setText("<html>" + message_builder.toString() + "</html>");
        message_pane.setCaretPosition(message_pane.getDocument().getLength());
        refreshContent();
    }

    public void setPlayerInfo(Player p) {
        player_info.setPlayer(p);
        //side_bar.repaint();
    }
    
    public void updatePlayerInfo() {
        player_info.refresh();
        //side_bar.repaint();
    }
    
//    public void updateDice() {
//        player_info.updateDice(data.getDice());
//    }
    

    
}
